import { expect } from '@jest/globals';
import path from 'path';

import { runHvigorAsync } from '../../../common/common';
import { commonFileOperation } from '../../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType, SignType,
} from '../../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  }
};

const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L2,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble,
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();

  // 修改 工程目录下的 hvigorfile.ts
  await commonFileOperation(path.resolve(projectRoot, 'hvigorfile.ts'), async (buffer) => {
    const content = buffer.toString();
    return 'import { hvigor } from \'@ohos/hvigor\'; \n' +
    `${content} ` +
      '\n console.log(\'hvigor.getParameter().getProperty(\"Ckey1\"):\' ,hvigor.getParameter().getProperty(\"Ckey1\"));' +
      '\n console.log(\'hvigor.getParameter().getProperty(\"Ckey2\"):\' ,hvigor.getParameter().getProperty(\"Ckey2\"));' +
      '\n console.log(\'hvigor.getParameter().getProperty(\"enableSignTask\"):\' ,\'\'+hvigor.getParameter().getProperty(\"enableSignTask\"));' +
      '\n console.log(\'hvigor.getParameter().getExtParam(\"Pkey1\") :\' ,hvigor.getParameter().getExtParam(\"Pkey1\"));' +
      '\n console.log(\'hvigor.getParameter().getExtParam(\"Pkey2\") :\' ,hvigor.getParameter().getExtParam(\"Pkey2\"));' +
      '\n console.log(\'hvigor.getParameter().getExtParam(\"product\") :\' ,hvigor.getParameter().getExtParam(\"product\"));';
  });

  const commands: string[] = '--mode project -p product=default -p Pkey1=Pvalue1 -p Pkey2=Pvalue2 -c properties.Ckey1=Cvalue1 -c properties.Ckey2=Cvalue2 -c properties.enableSignTask=false assembleApp --analyze --parallel --incremental --daemon'.split(' ');

  const result1 = await runHvigorAsync(projectRoot, commands,undefined,'@ohos/hvigor/bin/hvigor');
  record.addStep('step1', result1);

  expect(result1.stdout).toContain('hvigor.getParameter().getProperty("Ckey1"): Cvalue1');
  expect(result1.stdout).toContain('hvigor.getParameter().getProperty("Ckey2"): Cvalue2');
  expect(result1.stdout).toContain('hvigor.getParameter().getProperty("enableSignTask"): false');
  expect(result1.stdout).toContain('hvigor.getParameter().getExtParam("Pkey1") : Pvalue1');
  expect(result1.stdout).toContain('hvigor.getParameter().getExtParam("Pkey2") : Pvalue2');
  expect(result1.stdout).toContain('hvigor.getParameter().getExtParam("product") : default');
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('customTasks01')
  .description('custom tasks -p -c params test')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
